Constructor | |
---|---|
Canvas(master,**options) | Constructor |
Options | |
---|---|
bd | Border width in pixels. Default is 2. |
bg | Background color. |
cursor | Cursor used in the canvas like arrow, circle, dot etc |
height ,width | Size of the canvas. |
Methods: | |
---|---|
create_rectangle(*args[x1,y1,x2,y2],**kw[width,fill,outline]) | Create rectangle with coordinates x1,y1,x2,y2. |
create_oval(*args[x1,y1,x2,y2],**kw[width,fill,outline]) | Create oval with coordinates x1,y1,x2,y2. |
create_polygon(*args[x1,y1,x2,y2,….xn,yn],**kw[width,fill,outline]) | Create polygon with coordinates x1,y1,...,xn,yn. |
create_arc(*args[x1,y1,x2,y2], **kw[start,extent,width,fill,outline]) | Create arc shaped region with coordinates x1,y1,x2,y2. |
create_line( *args[x1,y1,x2,y2,….xn,yn], **kw[fill,width]) | Create line with coordinates x1,y1,...,xn,yn. |
create_text(*args[x,y],**kw[text,fill,font]) |
|
create_image(x , y , anchor=pos, image=imgObj) | To display image. |
from tkinter import * class MyFrame(Tk): def __init__(self): super().__init__() self.can=Canvas(self,width=200,height=200,bg="white") self.can.pack(padx=10,pady=10) self.can.create_oval([0,0,200,200],fill="orange",outline ="red",width=5) frm=MyFrame() frm.mainloop()
from tkinter import * class MyFrame(Tk): def __init__(self): super().__init__() self.can=Canvas(self,width=400,height=400,bg="white") self.can.pack() self.can.create_rectangle([10,10,190,190],fill="red") self.can.create_rectangle([210,10,390,190],fill= "yellow") self.can.create_rectangle([10,210,190,390],fill="green") self.can.create_rectangle([210,210,390,390],fill="blue") frm=MyFrame() frm.mainloop()
from tkinter import * class MyFrame(Tk): def __init__(self): super().__init__() self.can=Canvas(self,width=200,height=200,bg="white") self.can.pack() for i in range(0,100,10): x1=i y1=i x2=200-i y2=200-i self.can.create_oval([x1,y1,x2,y2]) frm=MyFrame() frm.mainloop()
from tkinter import * class MyFrame(Tk): def __init__(self): super().__init__() self.can=Canvas(self,width=200,height=200,bg="white") self.can.pack(padx=10,pady=10) j=1 for i in range(0,100,10): x1=i y1=i x2=200-i y2=200-i if j%2==0: self.can.create_oval([x1,y1,x2,y2],fill="red") else: self.can.create_oval([x1,y1,x2,y2],fill="blue") j+=1 frm=MyFrame() frm.mainloop()
from tkinter import * class MyFrame(Tk): def __init__(self): super().__init__() self.can=Canvas(self,width=400,height=400,bg="white") self.can.pack(padx=10,pady=10) lst=["red","orange","yellow","green","cyan","blue"] for i in range(0,36): self.can.create_arc([0,0,400,400],start=i*10,extent=10, fill=lst[i%6]) frm=MyFrame() frm.mainloop()
from tkinter import * class MyFrame(Tk): def __init__(self): super().__init__() dlst={"Amit":200,"Gopal":700,"Mohan":300,"Chetan":400,"Raj":500} clst=["red","green","orange","blue","yellow","pink","cyan"] self.can=Canvas(self,width=max(dlst.values()),height= len(dlst)*100,bg="white") self.can.pack(padx=10,pady=10) i=0 for nm,sales in dlst.items(): self.can.create_rectangle([0,i*100,sales,i*100+100], fill=clst[i]) fnt="arial 15 bold" self.can.create_text([75,i*100+50],text=nm+":" +str(sales),fnt,fill="white") i+=1 frm=MyFrame() frm.mainloop()